﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_PeriodicalItem")]
    public class T_PeriodicalItem : IEntity<T_PeriodicalItem>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public int PeriodicalId { get; set; }
        /// <summary>
        /// 本期刊号
        /// </summary>
        [Column(TypeName="varchar")]
        [StringLength(64)]
        public string Manuscriptcode { get; set; }

        public DateTime PublishDate { get; set; }
        public DateTime CreateDate { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string CoverImage { get; set; }

        [Column(TypeName = "text")]
        public string Information { get; set; }

        /// <summary>
        /// 0：筹备 1：已出版
        /// </summary>
        public int Status { get; set; }

        public override T_PeriodicalItem Find(System.Linq.Expressions.Expression<Func<T_PeriodicalItem, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.PeriodicalItem.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.PublishDate = item.PublishDate;
                    this.Manuscriptcode = item.Manuscriptcode;
                    this.CoverImage = item.CoverImage;
                    this.CreateDate = item.CreateDate;
                    this.Information = item.Information;
                    this.Status = item.Status;
                    this.PeriodicalId = item.PeriodicalId;
                    return this;
                }
                return null;
            }
        }
        public override T_PeriodicalItem Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }
        public override bool Any(System.Linq.Expressions.Expression<Func<T_PeriodicalItem, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.PeriodicalItem.Any(where);
            }
        }
    }
}
